package edu.utwente.sem.hrs;

import java.io.IOException;

import ocsf.server.AbstractServer;
import ocsf.server.ConnectionToClient;
import edu.utwente.sem.hrs.model.Hotel;

public class HotelServer extends AbstractServer
{
  public static final int DEFAULT_PORT = 5555;

  /**
   * This method is responsible for the creation of the server instance (there
   * is no UI in this phase).
   * 
   * @param args
   *          [0] The port number to listen on. Defaults to 5555 if no argument
   *          is entered.
   */
  public static void main(String[] args)
  {
    int port = 0; // Port to listen on

    try
    {
      port = Integer.parseInt(args[0]); // Get port from command line
    }
    catch (Throwable t)
    {
      port = DEFAULT_PORT; // Set port to 5555
    }

    HotelServer hs = new HotelServer(port);

    try
    {
      hs.listen(); // Start listening for connections
    }
    catch (Exception ex)
    {
      System.out.println("ERROR - Could not listen for clients!");
    }
  }
  private Hotel hotel;

  private CommandDispatcher cd;

  public HotelServer(int port)
  {
    super(port);
    this.hotel = Hotel.getInstance();
    this.cd = new CommandDispatcher(hotel);
  }

  /**
   * Hook method called each time a new client connection is accepted. The
   * default implementation does nothing.
   * 
   * @param client
   *          the connection connected to the client.
   */
  protected void clientConnected(ConnectionToClient client)
  {
    System.out.println("A client connected.");
    client.setInfo("loggedin", false);
  }

  /**
   * Hook method called each time this server disconnects a client. The default
   * implementation does nothing. The method may be overridden by subclasses but
   * should remains synchronized.
   * 
   * @param client
   *          the connection with the client.
   */
  synchronized protected void clientDisconnected(ConnectionToClient client)
  {
    System.out.println("A client disconnected.");
  }

  synchronized protected void clientException(ConnectionToClient client,
      Throwable exception)
  {
    try
    {
      client.close();
    }
    catch (IOException ex)
    {
      System.out.println("Error, a client has gone, but dont wanna go");

    }
  }

  /**
   * This method handles any messages received from the client.
   * 
   * @param msg
   *          The message received from the client.
   * @param client
   *          The connection from which the message originated.
   */
  public void handleMessageFromClient(Object msg, ConnectionToClient client)
  {
    Command c = cd.parseCommand((String) msg, client);
    String s = cd.execCommand(c);
    if (s != null && !s.isEmpty())
    {
      try
      {
        client.sendToClient(s);
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
  }

  public boolean isIngelogd(ConnectionToClient client)
  {
    return (boolean) client.getInfo("loggedin");
  }

  /**
   * This method overrides the one in the superclass. Called when the server
   * starts listening for connections.
   */
  protected void serverStarted()
  {
    System.out.println("Server listening for connections on port " + getPort());
  }

  /**
   * This method overrides the one in the superclass. Called when the server
   * stops listening for connections.
   */
  protected void serverStopped()
  {
    System.out.println("Server has stopped listening for connections.");
  }

  public String wieIsIngelogd(ConnectionToClient client)
  {
    return (String) client.getInfo("naam");
  }
}
